#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vmath.h>
#include <xmltree.h>

struct sphere_light {
	float rad;
};

void free_data(void *data)
{
	free(data);
}

void *read_xml(struct xml_node *node)
{
	struct sphere_light *sl;
	struct xml_node *sl_node;
	struct xml_attr *attr;

	sl_node = node->chld;
	while(sl_node && strcmp(sl_node->name, "spherelt") != 0) {
		sl_node = sl_node->next;
	}
	if(!sl_node) {
		return 0;
	}

	if(!(sl = malloc(sizeof *sl))) {
		return 0;
	}

	if((attr = xml_get_attr(sl_node, "rad"))) {
		sl->rad = attr->fval;
	} else {
		sl->rad = 1.0;
	}
	return sl;
}

struct xml_tree *write_xml(void *data)
{
	return 0;
}

void print_info(void *data, FILE *fp)
{
	fprintf(fp, "point light\n");
}

vec3_t get_point(void *data, mat4_t xform)
{
	vec3_t pt;
	struct sphere_light *sl = data;
	double len_sq;

	do {
		pt.x = frand(2.0 * sl->rad) - sl->rad;
		pt.y = frand(2.0 * sl->rad) - sl->rad;
		pt.z = frand(2.0 * sl->rad) - sl->rad;
		len_sq = v3_length_sq(pt);
	} while(len_sq > sl->rad * sl->rad);

	return v3_transform(pt, xform);
}
